package com.yyq.edumsm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.edumsm.entity.PhoneCode;
import com.yyq.edumsm.service.PhoneCodeService;
import com.yyq.edumsm.utils.HttpUtils;
import com.yyq.edumsm.utils.RandomUtils;
import com.yyq.result.R;
import com.yyq.servicebase.exceptionhandler.YyqException;
import org.apache.http.HttpResponse;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyq
 * @since 2021-04-28
 */
@CrossOrigin
@RestController
@RequestMapping("/edumsm/phone-code")
public class PhoneCodeController {
    @Autowired
    private PhoneCodeService service;


    //验证手机号和密码是否统一
    @GetMapping("changePhone/{phone}/{code}")
    public R changePhone(@PathVariable String phone,@PathVariable String code){
        if (phone.length()!=11) throw new YyqException(20001,"手机号位数错误");
        QueryWrapper<PhoneCode> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        wrapper.eq("code",code);
        PhoneCode one = service.getOne(wrapper);
        if (one==null) throw  new YyqException(20001,"验证码错误");

        return R.ok().data("flag",1);
    }


    //向数据库添加对应手机号得验证码 注册不能使用　
    @GetMapping("newCode/{phone}")
    public R getcode(@PathVariable String phone){
        //添加判断
        int flag=1;
        if(phone.length()!=11||phone==null){
            flag=0;
            throw new YyqException(20001,"手机号位数错误");
            //令flag=0代表手机号位数不

        }
        QueryWrapper<PhoneCode> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        service.remove(wrapper);


        String code= RandomUtils.getFourRandomNum();
        PhoneCode phoneCode=new PhoneCode();
        phoneCode.setCode(code);
        phoneCode.setMobile(phone);
        service.save(phoneCode);

        //向手机发送验证码
        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";
        String method = "POST";
        String appcode = "c1415493129344a2997843f4563cff83";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", "15303846538");
        querys.put("param", "code:"+code);
        querys.put("tpl_id", "TP1711063");
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok().data("flag",flag);
    }
    @Test
    public void run(){
        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";
        String method = "POST";
        String appcode = "c1415493129344a2997843f4563cff83";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", "15303846538");
        String code=RandomUtils.getFourRandomNum();
        querys.put("param", "code:"+code);
        querys.put("tpl_id", "TP1711063");
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

